package com.mdlib.helper.module.send.profile.fragments;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.ui.fragments.set.AboutUsFragment;
import com.mdlib.helper.ui.fragments.set.FeedBackFragment;
import com.mdlib.helper.ui.fragments.set.ModifyPasswordFragment;
import com.mdlib.helper.ui.fragments.set.ServiceFragment;
import com.mdlib.helper.utils.DataCleanManager;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/15 17:21
 * 邮箱：hqy_xz@126.com
 */

public class SettingFragment extends BaseTitleFragment {


    @Bind(R.id.rl_set_modify_password)
    RelativeLayout mRlSetModifyPassword;
    @Bind(R.id.rl_set_clear_cache)
    RelativeLayout mRlSetClearCache;
    @Bind(R.id.rl_set_feedback)
    RelativeLayout mRlSetFeedback;
    @Bind(R.id.rl_set_service)
    RelativeLayout mRlSetService;
    @Bind(R.id.rl_set_about_us)
    RelativeLayout mRlSetAboutUs;
    @Bind(R.id.tv_set_login_out)
    TextView mTvSetLoginOut;
    @Bind(R.id.tv_set_cache)
    TextView mTvSetCache;

    public static SettingFragment newInstance() {
        return new SettingFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_setting;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.me_tv_set, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        getCache();
    }

    @OnClick({R.id.rl_set_modify_password, R.id.rl_set_clear_cache, R.id.rl_set_feedback, R.id.rl_set_service, R.id.rl_set_about_us, R.id.tv_set_login_out})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_set_modify_password:
                addFragment(ModifyPasswordFragment.newInstance());
                break;
            case R.id.rl_set_clear_cache:
                DataCleanManager.clearAllCache(getActivity());
                getCache();
                break;
            case R.id.rl_set_feedback:
                addFragment(FeedBackFragment.newInstance());
                break;
            case R.id.rl_set_service:
                addFragment(ServiceFragment.newInstance());
                break;
            case R.id.rl_set_about_us:
                addFragment(AboutUsFragment.newInstance());
                break;
            case R.id.tv_set_login_out:
                loginOut();
                break;
        }
    }

    public void getCache() {
        try {
            mTvSetCache.setText(DataCleanManager.getTotalCacheSize(getActivity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void loginOut() {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .loginout().compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        stopLoadingDialog();
                        AccountModel.getInstance().clearCache();
                        UIHelper.showLogin(getActivity());
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                        stopLoadingDialog();
                    }
                }));
    }


}
